package com.lyf.scm.plan.api.controller;

import com.github.pagehelper.PageInfo;
import com.lyf.scm.common.model.ExcelExportDataDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.lyf.scm.plan.api.dto.DemandPlanInfoDTO;
import com.lyf.scm.plan.api.dto.SalesPlanDTO;
import com.lyf.scm.plan.api.dto.SalesPlanInfoDTO;
import com.lyf.scm.plan.domain.service.SalesPlanService;
import com.rome.arch.core.clientobject.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Martin
 * @date 2020-10-29 13:20
 */
@Slf4j
@Api(tags = "需求计划")
@RequestMapping("/api/v1/salesPlan")
@RestController
public class SalesPlanController {

    @Resource
    private SalesPlanService salesPlanService;

    @PostMapping("/listByWeek")
    @ApiOperation(value = "按条件查询销售计划")
    public Response<PageInfo<SalesPlanInfoDTO>> listWeekBySelective(@RequestBody SalesPlanDTO salesPlanDTO) {
        return Response.builderSuccess(salesPlanService.listWeekBySelective(salesPlanDTO));
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "查询需求范围指标(范围)")
    public Response<List<DemandPlanInfoDTO>> listAllByCycles(@RequestBody SalesPlanDTO salesPlanDTO){
        return Response.builderSuccess(salesPlanService.listAll(salesPlanDTO));
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void exportExcel(@RequestBody SalesPlanDTO salesPlanDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse){
        try{
            ExcelExportDataDTO excelExportDataDTO = salesPlanService.exportExcel(salesPlanDTO, type);
            ExcelExportUtil.exportExcel(httpServletResponse, excelExportDataDTO);
        }catch (Exception e){
            log.error("销售计划导出异常", e);
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }
}
